//
//  NewsClueRootVC.m
//  Yihubang
//
//  Created by xmy on 16/7/20.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "CenterAndDeterenceRootVC.h"
#import "MyCenterViewController.h"
#import "DeterenceRootViewController.h"
@interface CenterAndDeterenceRootVC ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource, UIScrollViewDelegate>

@property (nonatomic ,strong) UIPageViewController *mPageViewController;
@property (nonatomic ,strong) NSArray *viewControllerArr;
@property (nonatomic ,assign) NSInteger selectIndex;
@property (nonatomic ,strong) UIScrollView *pageScrollView;
@end

@implementation CenterAndDeterenceRootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleView];
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
//    [self doLogin];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void) setTitleView{
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"GuardMe" bundle:[NSBundle mainBundle]];
    MyCenterViewController * MyCenterViewController = [story instantiateViewControllerWithIdentifier:@"MyCenterViewController"];
    DeterenceRootViewController * DeterenceRootViewController = [HelpStory instantiateViewControllerWithIdentifier:@"DeterenceRootViewController"];
    self.viewControllerArr =@[DeterenceRootViewController,MyCenterViewController];
    self.mPageViewController =self.childViewControllers.firstObject;
//    for (id testView in self.mPageViewController.view.subviews) {
//        UIScrollView *scrollView = (UIScrollView *)testView;
//        if ([scrollView isKindOfClass:[UIScrollView class]]) {
//            scrollView.delegate = self;
//            // scrollView.bounces = self.shouldBounce;
//        }
//    }
    self.mPageViewController.delegate=self;
    self.mPageViewController.dataSource=self;
    [self.mPageViewController setViewControllers:@[DeterenceRootViewController]
                                       direction:UIPageViewControllerNavigationDirectionReverse
                                        animated:YES
                                      completion:nil];
}
-(NSArray *)viewControllerArr{
    if (!_viewControllerArr) {
        _viewControllerArr = [NSMutableArray array];
    }
    return _viewControllerArr;
}

//-(UIScrollView *)pageScrollView{
//    if (!_pageScrollView) {
//        for (id testView in self.mPageViewController.view.subviews) {
//            UIScrollView *scrollView = (UIScrollView *)testView;
//            if ([scrollView isKindOfClass:[UIScrollView class]]) {
//                _pageScrollView = scrollView;
//                // scrollView.bounces = self.shouldBounce;
//            }
//        }
//    }
//    return _pageScrollView;
//}

#pragma mark - UIPageViewControllerDataSource And UIPageViewControllerDelegate
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    NSInteger index = [self.viewControllerArr indexOfObject:viewController];
    if (index==0 || index == NSNotFound) {
        return nil;
    }else{
        return self.viewControllerArr[--index];
    }
}
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger index = [self.viewControllerArr indexOfObject:viewController];
    if (index == self.viewControllerArr.count-1) {
        return nil;
    }
    else{
        if ([self doLogin]) {
           return self.viewControllerArr[++index];
        }
        return nil;
    }
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    if (completed) {
        self.selectIndex =[self.viewControllerArr indexOfObject:previousViewControllers[0]];
    }
}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
{
    self.selectIndex = [self.viewControllerArr indexOfObject:pendingViewControllers[0]];
    
}
-(void)removePageDataSource{
    for (id testView in self.mPageViewController.view.subviews) {
        UIScrollView *scrollView = (UIScrollView *)testView;
        if ([scrollView isKindOfClass:[UIScrollView class]]) {
            scrollView.scrollEnabled = NO;
            // scrollView.bounces = self.shouldBounce;
        }
    }
}
-(void)resetPageDataSource{
    for (id testView in self.mPageViewController.view.subviews) {
        UIScrollView *scrollView = (UIScrollView *)testView;
        if ([scrollView isKindOfClass:[UIScrollView class]]) {
            scrollView.scrollEnabled = YES;
            // scrollView.bounces = self.shouldBounce;
        }
    }
}

-(void)removeCallHelpGuideViewActionAfter{
    [self.mPageViewController setViewControllers:@[self.viewControllerArr [1]]
                                       direction:UIPageViewControllerNavigationDirectionForward
                                        animated:YES
                                      completion:nil];
}

-(void)removeMyCenterGuideViewActionAfter{
    UIViewController *mainVct = [UIStoryboard viewController:@"GuardMe" identifier:@"CenterAndDeterenceRootNAV"];
    [self presentViewController:mainVct animated:NO completion:^(void){
    }];
}

-(void)dealloc{
}

@end
